package com.nuc.kl.util;

import java.util.Date;

import javax.annotation.Resource;

import org.apache.commons.codec.digest.DigestUtils;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.nuc.kl.module.basedata.entity.Direction;
import com.nuc.kl.module.basedata.entity.Navigation;
import com.nuc.kl.module.basedata.entity.ReferenceType;
import com.nuc.kl.module.kpoint.entity.Knowledge;
import com.nuc.kl.module.kpoint.entity.Reference;
import com.nuc.kl.module.usermanager.entity.Privilege;
import com.nuc.kl.module.usermanager.entity.Role;
import com.nuc.kl.module.usermanager.entity.Student;
import com.nuc.kl.module.usermanager.entity.Teacher;
import com.nuc.kl.module.usermanager.entity.User;

@Component
public class Installer {
	
	@Resource
	private SessionFactory sessionFactory;
	
	/**
	 * 执行安装
	 * 安装前先删除所有表
	 */
	@Transactional
	public void install(){
		
		Session session = sessionFactory.getCurrentSession();
		
		String md5Digest = DigestUtils.md5Hex("123123");
//		================================================================
		//初始化teacher
		Teacher teacher1,teacher2,teacher3,teacher4,teacher5;
		
		teacher1 = new Teacher("jml","jml",md5Digest,"中北大学",1);
		teacher2 = new Teacher("lb","lb",md5Digest,"中北大学",0);
		teacher3 = new Teacher("lhl","lhl",md5Digest,"中北大学",1);
		teacher4 = new Teacher("ysm","ysm",md5Digest,"中北大学",1);
		teacher5 = new Teacher("ysq","yqs",md5Digest,"中北大学",0);
		
		session.save(teacher1);
		session.save(teacher2);
		session.save(teacher3);
		session.save(teacher4);
		session.save(teacher5);
		
		
		//初始化role
		Role role1,role2,role3;
		role1 = new Role("知识点管理员","知识点管理员");
		role2 = new Role("基础设置管理员","基础设置管理员");
		role3 = new Role("用户管理员","用户管理员");
		
		session.save(role1);
		session.save(role2);
		session.save(role3);
		
		//初始化privilege
		Privilege privilege1,privilege2,privilege3,privilege4,privilege5,privilege6,privilege7,privilege8,privilege9,privilege10,
				privilege11,privilege12,privilege13,privilege14,privilege15,privilege16;
		privilege1 = new Privilege("知识管理",null,"closed",null);
		privilege2 = new Privilege("用户管理",null,"closed",null);
		privilege3 = new Privilege("基础数据",null,"closed",null);
		privilege4 = new Privilege("个人设置 ",null,"closed",null);
		
		session.save(privilege1);
		session.save(privilege2);
		session.save(privilege3);
		session.save(privilege4);
		
		
		//知识管理
		privilege5 = new Privilege("查看知识点","/kpoint_listUI",null,privilege1);
		privilege6 = new Privilege("查看参考资料","/reference_listUI",null,privilege1);
		privilege7 = new Privilege("建立学习计划(学生)",null,null,privilege1);
		privilege8 = new Privilege("辅助教案生成(教师)",null,null,privilege1);
		privilege9 = new Privilege("提交知识点","/kpoint_addUI",null,privilege1);
		privilege10 = new Privilege("知识点审核","/kpoint_checkUI",null,privilege1);
		privilege16 = new Privilege("回收站","/kpoint_reCycleUI",null,privilege1);
		
		session.save(privilege5);
		session.save(privilege6);
		session.save(privilege7);
		session.save(privilege8);
		session.save(privilege9);
		session.save(privilege10);
		session.save(privilege16);
		
		//用户管理
		privilege5 = new Privilege("教师管理","/teacher_list",null,privilege2);
		privilege6 = new Privilege("学生管理","/student_list",null,privilege2);
		privilege8 = new Privilege("角色管理","/role_list",null,privilege2);
		privilege9 = new Privilege("权限管理","/privilege_list",null,privilege2);
		privilege10 = new Privilege("管理员管理","/user_list",null,privilege2);
		
		session.save(privilege5);
		session.save(privilege6);
		session.save(privilege7);
		session.save(privilege8);
		session.save(privilege9);
		session.save(privilege10);
		
		//基础设置
		privilege11 = new Privilege("专业方向管理","/direction_list",null,privilege3);
		privilege12 = new Privilege("知识分类管理","/navigation_list",null,privilege3);
		privilege13 = new Privilege("参考资料类型管理","/referenceType_list",null,privilege3);
		privilege14 = new Privilege("审核人数及通过率设置","/check_set",null,privilege3);
		
		session.save(privilege11);
		session.save(privilege12);
		session.save(privilege13);
		session.save(privilege14);
		
		//个人设置
		privilege15 = new Privilege("个人信息管理","/private_list",null,privilege4);
		session.save(privilege15);
		
		//初始化referenceType
		ReferenceType referenceType1,referenceType2,referenceType3,referenceType4,referenceType5,referenceType6;
		referenceType1 = new ReferenceType("图书","图书");
		referenceType2 = new ReferenceType("视频","视频");
		referenceType3 = new ReferenceType("博客","博客");
		referenceType4 = new ReferenceType("论坛","论坛");
		referenceType5 = new ReferenceType("网站","网站");
		referenceType6 = new ReferenceType("软件","软件");
		
		session.save(referenceType1);
		session.save(referenceType2);
		session.save(referenceType3);
		session.save(referenceType4);
		session.save(referenceType5);
		session.save(referenceType6);
		
		//初始化reference
		Reference reference1,reference2,reference3,reference4;
		reference1 = new Reference("百度","自己是最好的老师，动手搜搜看","www.baidu.com",new ReferenceType(5l));
		reference2 = new Reference("《疯狂java系列丛书》","李刚带领你，疯狂学java","",new ReferenceType(1l));
		reference3 = new Reference("《细说PHP》","兄弟连高洛峰亲情细说PHP里面故事","",new ReferenceType(1l));
		reference4 = new Reference("CSDN","程序员的天堂","www.csdn.net",new ReferenceType(5l));
		
		session.save(reference1);
		session.save(reference2);
		session.save(reference3);
		session.save(reference4);
		
		
//		================================================================
		//初始化direction
		Direction direction1,direction2,direction3,direction4,direction5,direction6,direction7;
		direction1 = new Direction("软件开发与测试","软件开发与测试方向");
		direction2 = new Direction("数字媒体技术","数字媒体技术方向");
		direction3 = new Direction("企业信息化软件开发与应用","企业信息化软件开发与应用方向");
		direction4 = new Direction("电子商务软件开发与应用","电子商务软件开发与应用方向");
		direction5 = new Direction("物流管理软件开发与应用","物流管理软件开发与应用方向");
		direction6 = new Direction("嵌入式软件开发","嵌入式软件开发方向");
		direction7 = new Direction("机电一体化软件开发与应用","机电一体化软件开发与应用方向");
		
		session.save(direction1);
		session.save(direction2);
		session.save(direction3);
		session.save(direction4);
		session.save(direction5);
		session.save(direction6);
		session.save(direction7);
		
		
		//初始化student
		Student student1,student2,student3,student4,student5;
		student1 = new Student("tff", "tff", md5Digest, "1121010101",new Direction(1l), "2011");
		student2 = new Student("lff", "lff", md5Digest, "1121010102",new Direction(2l), "2011");
		student3 = new Student("mff", "mff", md5Digest, "1121010103",new Direction(3l), "2011");
		student4 = new Student("wf", "wf", md5Digest, "1121010104",new Direction(4l), "2011");
		student5 = new Student("tef", "tef", md5Digest, "1121010105",new Direction(1l), "2011");
		
		session.save(student1);
		session.save(student2);
		session.save(student3);
		session.save(student4);
		session.save(student5);
		
		//初始化user其他管理员
		User user,user1,user2,user3,user4,user5;
		
		user = new User("admin","admin",md5Digest,1,"17003510000","03510000@170.com");
		user1 = new User("zy","张赟",md5Digest,1,"17003511000","03511000@170.com");
		user2 = new User("wr","王瑞",md5Digest,1,"17003511001","03511001@170.com");
		user3 = new User("lhj","雒怀杰",md5Digest,1,"17003511002","03511002@170.com");
		user4 = new User("zs","张三",md5Digest,0,"17003511003","03511003@170.com");
		user5 = new User("ls","李四",md5Digest,0,"17003511005","03511004@170.com");
		
		session.save(user);
		session.save(user1);
		session.save(user2);
		session.save(user3);
		session.save(user4);
		session.save(user5);
		
		
		//初始化navigation
		Navigation navigation1,navigation2,navigation3,navigation4,navigation5,navigation6,navigation7,navigation8,navigation9,navigation10,
				navigation11,navigation12,navigation13,navigation14,navigation15,navigation16,navigation17,navigation18,navigation19,navigation20,
				navigation21,navigation22,navigation23,navigation24,navigation25;
		navigation1 = new Navigation("编程语言","编程语言",new Date(),"closed",user,null);
		navigation2 = new Navigation("数据库","数据库",new Date(),"closed",user,null);
		navigation3 = new Navigation("建模","建模",new Date(),"closed",user,null);
		navigation4 = new Navigation("编程思想","编程思想",new Date(),"closed",user,null);
		navigation5 = new Navigation("项目","项目",new Date(),"closed",user,null);
		
		session.save(navigation1);
		session.save(navigation2);
		session.save(navigation3);
		session.save(navigation4);
		session.save(navigation5);
		
		//初始化编程语言
		navigation6 = new Navigation("java","java",new Date(),"closed",user,new Navigation(1l));
		navigation7 = new Navigation("PHP","PHP",new Date(),"open",user,new Navigation(1l));
		navigation8 = new Navigation("Android","Android",new Date(),"open",user,new Navigation(1l));
		navigation9 = new Navigation("C++","C++",new Date(),"open",user,new Navigation(1l));
		navigation10 = new Navigation("C语言","C语言",new Date(),"open",user,new Navigation(1l));
		navigation11 = new Navigation(".NET",".NET",new Date(),"open",user,new Navigation(1l));
		
		session.save(navigation6);
		session.save(navigation7);
		session.save(navigation8);
		session.save(navigation9);
		session.save(navigation10);
		session.save(navigation11);
		
		//初始数据库
		navigation12 = new Navigation("Oracal","Oracal",new Date(),"open",user,new Navigation(2l));
		navigation13= new Navigation("SqlServer","SqlServer",new Date(),"open",user,new Navigation(2l));
		navigation14 = new Navigation("SqlServer","SqlServer",new Date(),"open",user,new Navigation(2l));
		navigation15 = new Navigation("NoSql","NoSql",new Date(),"open",user,new Navigation(2l));
		
		session.save(navigation12);
		session.save(navigation13);
		session.save(navigation14);
		session.save(navigation15);
		
		//初始化java
		navigation16 = new Navigation("java基础","java基础",new Date(),"open",user,new Navigation(6l));
		navigation17 = new Navigation("javaWeb","javaWeb",new Date(),"open",user,new Navigation(6l));
		navigation18 = new Navigation("j2ee","j2ee",new Date(),"open",user,new Navigation(6l));
		
		session.save(navigation16);
		session.save(navigation17);
		session.save(navigation18);
		
		//初始化j2ee
		navigation19 = new Navigation("struts","struts",new Date(),"open",user,new Navigation(18l));
		navigation20 = new Navigation("hibernate","hibernate",new Date(),"open",user,new Navigation(18l));
		navigation21 = new Navigation("spring","spring",new Date(),"open",user,new Navigation(18l));
		navigation22 = new Navigation("mybatis","mybatis",new Date(),"open",user,new Navigation(18l));
		navigation23 = new Navigation("maven","maven",new Date(),"open",user,new Navigation(18l));
		navigation24 = new Navigation("webService","webService",new Date(),"open",user,new Navigation(18l));
		navigation25 = new Navigation("jbpm","jbpm",new Date(),"open",user,new Navigation(18l));
		
		session.save(navigation19);
		session.save(navigation20);
		session.save(navigation21);
		session.save(navigation22);
		session.save(navigation23);
		session.save(navigation24);
		session.save(navigation25);
		
		Knowledge knowledge1,knowledge2,knowledge3,knowledge4,knowledge5,
			knowledge6,knowledge7,knowledge8,knowledge9,knowledge10,
			knowledge11,knowledge12,knowledge13,knowledge14,knowledge15,
			knowledge16,knowledge17,knowledge18,knowledge19,knowledge20;
		
		knowledge1 = new Knowledge("oop","面向对象的编程思想","OOP三要素：封装、继承、多态。OOP比你想象的要难的多OOP的知识结构层次：层次1：把业务模型封装为对象;层次2：构建对象之间合理的关系;层次3：用合理的设计模式构建业务系统","中等","了解","0.5",new Navigation(15l),1,null,null,null);
		knowledge2 = new Knowledge("软件设计模式","软件设计模式","层次1：设计模式战术：类似武功技能，例如单例设计模式;层次2：设计模式战略：类似兵书战法，例如MVC设计模式","中等","了解","0.5",new Navigation(15l),1,null,null,null);
		knowledge3 = new Knowledge("OGNL上下文","存放数据的区域","数据类型： java.util.Map，以“键-值”形式存放数据，存放数据时，指定键获取数据时，通过键根对象： OGNL上下文中默认使用的第一个对象","中等","了解","0.5",new Navigation(15l),1,null,null,null);
		knowledge4 = new Knowledge("I18N","国际化","一个Web应用程序在运行时能够根据客户端请求所来自的国家和语言显示不同的用户界面。这样 ，当需要在应用程序中添加对一种新的语言的支持时，无需修改应用程序的代码。", "中等","了解","0.5",new Navigation(15l),1,null,null,null);
		knowledge5 = new Knowledge("ORM","是一种为了解决面向对象与关系型数据库存在的互不匹配的现象的技术","ORM是通过使用描述对象和数据库之间映射的元数据，将Java程序中的对象自动持久化到关系数据中。本质上就是将数据从一种形式转换到另外一种形式。","中等","了解","0.5",new Navigation(20l),1,null,null,null);
		knowledge6 = new Knowledge("ACID","ACID","原子性 Atomic,一致性 Consistency,独立性 Indenpency,持久性 Durability","中等","了解","0.5",new Navigation(20l),1,null,null,null);
		knowledge6 = new Knowledge("AOP","面向切面编程"," a)	面向过程、面向对象、面向接口、面向抽象    b)	是对面向对象的思维方式的有力补充","中等","了解","0.5",new Navigation(21l),1,null,null,null);
		knowledge7 = new Knowledge("BPM","BPM（Business Process Management）：业务流程管理","BPM就是一套对企业各个业务环节进行整合的全面的管理模式。","中等","了解","0.5",new Navigation(25l),1,null,null,null);
		knowledge8 = new Knowledge("泛型","泛型是jdk1.5之后出现的一个新特性","在jdk1.5推出的众多新特性，例如自动装拆箱、可变长参数、静态导入等等当中，泛型是最重要的","中等","了解","0.5",new Navigation(16l),1,null,null,null);
		knowledge9 = new Knowledge("jBPM","java Business Process Management","是基于java的业务流程管理系统。jBPM是市面上相当流行的一款开源工作流引擎，引擎底层基于Active Diagram模型。jBPM4.4使用了hibernate（3.3.1版），因此可以很好的支持主流数据库。jBPM4.4共有18张表。","中等","了解","0.5",new Navigation(25l),1,null,null,null);
		knowledge10 = new Knowledge("oop","面向对象的编程思想","OOP三要素：封装、继承、多态。OOP比你想象的要难的多OOP的知识结构层次：层次1：把业务模型封装为对象;层次2：构建对象之间合理的关系;层次3：用合理的设计模式构建业务系统","中等","了解","0.5",new Navigation(25l),1,null,null,null);
		knowledge11 = new Knowledge("Jdk","java development kit","包含开发java程序以及编译运行java程序所需要的所有资源。","简单","了解","0.5",new Navigation(16l),1,null,null,null);
		knowledge12 = new Knowledge("Jre","Java Runtime Environment java","就是如果你不开发java程序，只是要运行java程序的话有jre就可以了。","中等","了解","0.5",new Navigation(16l),1,null,null,null);
		knowledge13 = new Knowledge("数组","同一类型数据的组合","数组语法：在任意数据类型后边加[]。dataType [] arrayName;dataType arrayName[];其中每一个数据的类型实际是数组去掉一个[]。","中等","了解","0.5",new Navigation(16l),1,null,null,null);
		knowledge14 = new Knowledge("封装","封装是现实世界见的最多的现象","封装是俩个方向，合理隐藏，合理暴露。封装带来的好处也是俩方面：使用方便 (使用者，客户端)保护数据完整性。（服务提供者，开发者）","中等","了解","0.5",new Navigation(16l),1,null,null,null);
		knowledge15 = new Knowledge("继承","Java当中继承是类与类之间的关系","通过extends关键字实现，但是翻译成继承不合适，实际应该是扩展","中等","了解","0.5",new Navigation(16l),1,null,null,null);
		knowledge16 = new Knowledge("重载vs重写","子类可以重写（Override writer）或者覆盖父类的方法。和方法重载（Overload load）","重载：同一个类中的不同参数列表的同名方法。（俩同一不同）重写：俩同，俩大一小父子类中的方法。重写要求子类必须能够访问到该方法。不满意重写。如果父类方法private，你访问不到，根本就不是重写。","中等","了解","0.5",new Navigation(16l),1,null,null,null);
		knowledge17 = new Knowledge("多态","	同一类的事物表现出来的行为特征不一样","同一类型的变量在执行同一方法的时候，表现出来不同的行为特征。谓之多态","中等","了解","0.5",new Navigation(16l),1,null,null,null);
		knowledge18 = new Knowledge("Final关键字","final   finally（try catch finally） finilize（垃圾回收） 三个的比较","Final关键字可以用来修饰class，field，method。Final用来修饰类，该类不能被继承。Final用来修饰方法，该方法不可以不子类重写。Final用来修饰field，该field的值不可改变","中等","了解","0.5",new Navigation(16l),1,null,null,null);
		knowledge19 = new Knowledge("abstract(抽象的)","	可以用来修饰类和方法。不能用来修饰其余成分","abstract修饰类的话 类是抽象类 修饰方法的话，方法是抽象方法。","中等","了解","0.5",new Navigation(16l),1,null,null,null);
		knowledge20 = new Knowledge("接口","接口就是俩方的通信规范。或者协议，或者标准","定义接口实际就是定义具备某种资质需要能做什么事情（方法）。实现接口就是考证。实现接口以后就好比你考试通过，具备某种资质，可以合法的做某些事情。","中等","了解","0.5",new Navigation(16l),1,null,null,null);
		
		session.save(knowledge1);
		session.save(knowledge2);
		session.save(knowledge3);
		session.save(knowledge4);
		session.save(knowledge5);
		session.save(knowledge6);
		session.save(knowledge7);
		session.save(knowledge8);
		session.save(knowledge9);
		session.save(knowledge10);
		session.save(knowledge11);
		session.save(knowledge12);
		session.save(knowledge13);
		session.save(knowledge14);
		session.save(knowledge15);
		session.save(knowledge16);
		session.save(knowledge17);
		session.save(knowledge18);
		session.save(knowledge19);
		session.save(knowledge20);
		
		
	}
	
	public static void main(String[] args) {
		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");;
		Installer installer = (Installer) ac.getBean("installer");
		installer.install();
	}
	
	
	
}
